package com.qyer.commons.logger;

import static com.qyer.commons.utils.GenericConstants.SEPARATOR;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qyer.commons.model.User;
import com.qyer.commons.param.HttpParameter;
import com.qyer.commons.report.ReportItem;
import com.qyer.commons.utils.JsonOperator;
import org.slf4j.Logger;

/**
 * User: Z J Wu Date: 2016/04/11 Time: 17:38 Package: com.qyer.commons.logger
 */
public abstract class ContentShowLogger<T extends ReportItem> implements AppendOtherInfo<T> {

  protected Logger LOGGER;

  public ContentShowLogger() {
  }

  public void logContent(T t) {
    if (t == null) {
      return;
    }

    StringBuilder sb = new StringBuilder();
    HttpParameter parameter = t.getParameter();
    User user = parameter.getUser();

    sb.append(parameter.getRequestId());
    sb.append(SEPARATOR);
    sb.append(parameter.getContext());
    sb.append(SEPARATOR);
    sb.append(t.getStrategy());
    sb.append(SEPARATOR);
    sb.append(user.getUid());
    sb.append(SEPARATOR);
    sb.append(user.getUniqueId());
    sb.append(SEPARATOR);
    try {
      sb.append(JsonOperator.getInstance().toJson(parameter));
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
    sb.append(SEPARATOR);
    appendOtherInfo(sb, t);
    LOGGER.info(sb.toString());
  }
}
